// 封装购物车模块

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useUserStore } from './userStore'
import { insertCartAPI, getCartListAPI, delCartAPI } from '@/apis/cart'

export const useCartStore = defineStore('cart', () => {
    // 1.定义state 
    const cartList = ref([])

    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)

    // 2.定义action
    const addCart = async (goods) => {
        const { skuId, count } = goods
        // 登录后的逻辑
        if (isLogin.value) {
            await insertCartAPI({skuId, count})
            getCartList()
        } else {
            // 未登录，保存本地
            // 判断购物车中是否有该商品，有则count + 1        
            // 如果购物车中没有该商品，则直接push
            const item = cartList.value.find(item => item.skuId === goods.skuId)
            if (!item) {
                cartList.value.push(goods)
            } else {
                item.count += goods.count
            }
        }

    }

    // 删除购物车
    const delCart = async (skuId) => {
        // 通过下标删除
        // const idx = cartList.value.findIndex(item => item.skuId === skuId)
        // cartList.value.splice(idx, 1)

        if (isLogin.value) {
            // 登录，调用接口
            await delCartAPI([skuId])
            getCartList()
        } else {
            // 通过filter删除
            cartList.value = cartList.value.filter(item => item.skuId !== skuId)
        }
    }

    // 去除购物车
    const clearCart = async () => {
        cartList.value = []
    }

    // 获取购物车列表
    const getCartList = async () => {
        const res = await getCartListAPI()
        cartList.value = res.result
    }

    // 选中状态
    const signleSelected = (skuId, selected) => {
        const item = cartList.value.find(item => item.skuId === skuId)
        if (item) {
            item.selected = selected
        }
    }

    // 全选状态
    const totalSelected = (selected) => {
        cartList.value.forEach(item => item.selected = selected)
    }

    // 3.计算属性
    // 总数
    const total = computed(() => {
        // console.log(cartStore.cartList)
        return cartList.value.reduce((pre, item) => pre + item.count, 0)
    })

    // 总金额
    const totalMoney = computed(() => {
        return cartList.value.reduce((pre, item) => pre + item.price * item.count, 0).toFixed(2)
    })

    // 选中的 总金额，保留两位小数
    const totalSelectedMoney = computed(() => {
        return cartList.value.filter(item => item.selected).reduce((pre, item) => pre + item.price * item.count, 0).toFixed(2)
    })

    // 选中状态
    const totalSelectedSum = computed(() => {
        return cartList.value.filter(item => item.selected).reduce((pre, item) => pre + item.count, 0)
    })

    // 是否全选
    const isSelectAll = computed(() => {
        return cartList.value.every(item => item.selected)
    })

    return {
        cartList,
        getCartList,
        addCart,
        delCart,
        clearCart,
        signleSelected,
        totalSelected,
        total,
        totalMoney,
        totalSelectedMoney,
        totalSelectedSum,
        isSelectAll
    }
},
    {
        persist: true
    }
)